package com.example.spring_gobang.common;

import com.example.spring_gobang.model.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.catalina.User;

/**
 * 判断用户是否已经登录
 */
public class UserSessionUtils {
    public static UserInfo getSessionUser(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if (session != null &&
                    session.getAttribute(AppVariable.USER_SESSION_KEY) != null){
            //此时说明用户已经登录
            if (session.getAttribute(AppVariable.USER_SESSION_KEY) instanceof UserInfo){
                return (UserInfo) session.getAttribute(AppVariable.USER_SESSION_KEY);
            }else {
                return null;
            }
        }
        return null;
    }
}
